package com.orangelife.webservice.service.propFeeOffsetInfo.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.cerHouseholder.ICerHouseholderService;
import com.orangelife.service.cerHouseholder.model.CerHouseholderDto;
import com.orangelife.service.offset.IOffsetService;
import com.orangelife.service.propFeeOffsetInfo.IPropFeeOffsetInfoService;
import com.orangelife.service.propFeeOffsetInfo.model.PropFeeOffsetInfoDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.base.BaseService;
import com.orangelife.webservice.base.PageUtils;
import com.orangelife.webservice.service.propFeeOffsetInfo.IPropFeeOffsetInfoWebService;
import com.orangelife.webservice.service.propFeeOffsetInfo.entity.OffSetDto;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation 2015. All Rights Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * propFeeOffsetInfo类 的WebService基本作业
 * 
 * @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-11-24 14:18:55
 * ==============================================================
 */
@Service("webservice_propFeeOffsetInfo")
public class PropFeeOffsetInfoWebServiceImpl extends BaseService implements
		IPropFeeOffsetInfoWebService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "propFeeOffsetInfoService")
	private IPropFeeOffsetInfoService propFeeOffsetInfoService;
	@Resource(name = "offsetService")
	private IOffsetService offsetService;
	@Resource(name = "cerHouseholderService")
	private ICerHouseholderService cerHouseholderService;

	@Override
	public Response addPropFeeOffsetInfo(HttpServletRequest servletRequest,
			PropFeeOffsetInfoDto propFeeOffsetInfo) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		propFeeOffsetInfo.setCreateuserID(pageUtils.getBaseSession()
				.getUserID());
		int result = propFeeOffsetInfoService
				.addPropFeeOffsetInfo(propFeeOffsetInfo);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");

		return this.normalResp();
	}

	@Override
	public Response editPropFeeOffsetInfo(HttpServletRequest servletRequest,
			PropFeeOffsetInfoDto propFeeOffsetInfo) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(propFeeOffsetInfo.getOffsetID(),
				"offsetID");
		if (error != null) {
			return error;
		}

		/************************************************
		 * 输入参数检查
		 ************************************************/
		// TODO;

		propFeeOffsetInfo.setEdituserID(pageUtils.getBaseSession().getUserID());
		int result = propFeeOffsetInfoService
				.editPropFeeOffsetInfo(propFeeOffsetInfo);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response deletePropFeeOffsetInfo(HttpServletRequest servletRequest,
			String offsetID) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(offsetID, "offsetID");
		if (error != null) {
			return error;
		}
		int result = propFeeOffsetInfoService
				.deletePropFeeOffsetInfoByLogic(offsetID);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "删除实体成功");

		return this.normalResp();
	}

	@Override
	public Response findPropFeeOffsetInfoByID(
			HttpServletRequest servletRequest, String offsetID)
			throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(offsetID, "offsetID");
		if (error != null) {
			return error;
		}
		PropFeeOffsetInfoDto propFeeOffsetInfo = propFeeOffsetInfoService
				.findPropFeeOffsetInfoByID(offsetID);

		Map<String, Object> maps = new HashMap<String, Object>();

		if (propFeeOffsetInfo != null) {

		}

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "查询实体成功");
		return this.normalResp(propFeeOffsetInfo);
	}

	@Override
	public Response getAllPropFeeOffsetInfo(HttpServletRequest servletRequest,
			String commID, String searchType, String startTime, String endTime,
			String search, String tsCreate, String pageIndex, String pageSize)
			throws Exception {

		PageUtils pageUtils = this.validatePageAndSetPage(servletRequest,
				pageIndex, pageSize);
		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (!StringUtils.isBlank(tsCreate)) {
			StringUtils.isTime(tsCreate);
			tsCreate = String.valueOf(Long.parseLong(tsCreate) / 1000);
		}
		/************************************************
		 * 查询列表
		 ************************************************/
		List<Map<String, Object>> list = propFeeOffsetInfoService
				.findPropFeeOffsetInfoByPage(commID, searchType, startTime,
						endTime, tsCreate == null ? null : tsCreate,
						search == null ? null : search,
						pageUtils.getiPageIndex(), pageUtils.getiPageSize());

		/************************************************
		 * 返回结果设定
		 ************************************************/
		Long total = ((Page<Map<String, Object>>) list).getTotal();

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginName() + new Date()
				+ "查询实体成功");

		return this.normalResp(pageUtils.getiPageIndex(),
				pageUtils.getiPageSize(), total,
				PropertyUtils.turnToEntity(list));
	}

	@Override
	public Response bauthFindPropFeeOffsetInfo(
			HttpServletRequest servletRequest, List<String> propFeeOffsetInfo)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeOffsetInfo == null || propFeeOffsetInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", propFeeOffsetInfoService
					.bauthfindPropFeeOffsetInfo(propFeeOffsetInfo));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthFindPropFeeOffsetInfos(
			HttpServletRequest servletRequest,
			Map<String, Object> propFeeOffsetInfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();

		if (!StringUtils.isBlank(StringUtils.toString(propFeeOffsetInfo
				.get("ispage")))) {

			propFeeOffsetInfo.put("pageIndex", StringUtils.toString(
					propFeeOffsetInfo.get("pageIndex"), "1"));
			propFeeOffsetInfo.put("pageSize", StringUtils.toString(
					propFeeOffsetInfo.get("pageSize"), "10"));

		}
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeOffsetInfo == null || propFeeOffsetInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity",
					PropertyUtils.turnToEntity(propFeeOffsetInfoService
							.bauthfindPropFeeOffsetInfoByMap(propFeeOffsetInfo)));

			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response clearCache(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		propFeeOffsetInfoService.clearCache();
		return this.normalResp();
	}

	@Override
	public Response autoOffset(HttpServletRequest servletRequest,
			String apartmentID, String commID) throws Exception {

		offsetService.autoOffset(commID, apartmentID, null, null);

		return this.normalResp();
	}

	@Override
	public Response bauthFindDetail(HttpServletRequest servletRequest,
			Map<String, Object> propFeeOffsetInfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeOffsetInfo == null || propFeeOffsetInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", PropertyUtils
					.turnToEntity(propFeeOffsetInfoService
							.getOffDetail(propFeeOffsetInfo)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response getOffTime(HttpServletRequest servletRequest,
			Map<String, Object> propFeeOffsetInfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeOffsetInfo == null || propFeeOffsetInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("entity", PropertyUtils
					.turnToEntity1(propFeeOffsetInfoService
							.getOffTime(propFeeOffsetInfo)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthSearchPropFeeDetails(
			HttpServletRequest servletRequest, Map<String, Object> propFeeDetail)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeDetail == null || propFeeDetail.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", PropertyUtils
					.turnToEntity(propFeeOffsetInfoService
							.bauthSearchPropFeeDetails(propFeeDetail)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response getAllPropFeeOffsetInfoByApartment(
			HttpServletRequest servletRequest,String status, String commID,
			String apartmentID, String startTime, String endTime,
			String search, String tsCreate, String pageIndex, String pageSize)
			throws Exception {

		// 获取手机号
		/************************************************
		 * 检查session
		 ************************************************/

		PageUtils pageUtils = this.validatePageAndSetPage(servletRequest,
				pageIndex, pageSize);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		List<CerHouseholderDto> c = cerHouseholderService
				.findCerHouseholderByRelevantID(null, apartmentID, null, null,
						null, pageUtils.getBaseSession().getLoginName(), null);

		if (c == null || c.size() <= 0) {

			return this.abnormalResp(2, "非认证用户");
		}

		List<Map<String, Object>> list = propFeeOffsetInfoService
				.selectOffsetInfoForApp(status,null, commID, apartmentID, null, null,
						"1,3", pageUtils.getiPageIndex(),
						pageUtils.getiPageSize());
		// TODO Auto-generated method stub

		Long total = ((Page<Map<String, Object>>) list).getTotal();

		return this.normalResp(pageUtils.getiPageIndex(),
				pageUtils.getiPageSize(), total,
				PropertyUtils.turnToEntity(list));
	}

	@Override
	public Response getAllPropFeeOffsetInfoByMap(
			HttpServletRequest servletRequest, Map<String, Object> params)
			throws Exception {
		int pageIndex = 0;
		int pageSize = 10;
		if (!StringUtils.isBlank(StringUtils.toString(params.get("ispage")))) {

			params.put("pageIndex",
					StringUtils.toString(params.get("pageIndex"), "1"));
			params.put("pageSize",
					StringUtils.toString(params.get("pageSize"), "10"));
			pageIndex = Integer.parseInt(params.get("pageIndex").toString());
			pageSize = Integer.parseInt(params.get("pageSize").toString());

		}

		List<Map<String, Object>> list = propFeeOffsetInfoService
				.getOffsetInfoByMap(params);

		long total = 0l;
		if (!StringUtils.isBlank(StringUtils.toString(params.get("ispage")))) {

			total = ((Page) list).getTotal();
		}

		// TODO Auto-generated method stub
		return this.normalResp(pageIndex, pageSize, total,
				PropertyUtils.turnToEntity(list));
	}

	@Override
	public Response cancelOffset(HttpServletRequest servletRequest,
			OffSetDto offSetDto) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		if (StringUtils.isBlank(offSetDto.getCommID())) {

			return this.abnormalResp(-1, "小区ID不能为空");
		}
		if (offSetDto.getOffsetIDs() == null
				|| offSetDto.getOffsetIDs().length == 0) {

			return this.abnormalResp(-1, "撤销id不能为空");
		}
		offSetDto.setCancelManID(pageUtils.getBaseSession().getUserID());
		offSetDto.setCancelMan(pageUtils.getBaseSession().getRealname());
		offsetService.cancelOffSet(offSetDto);
		// TODO Auto-generated method stub
		return this.normalResp();
	}

	@Override
	public Response getOffsetInfo(HttpServletRequest servletRequest,
			String orderID) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			map.put("pageEntity", PropertyUtils
					.turnToEntity(propFeeOffsetInfoService.getOffsetInfos(
							orderID, null)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}
}
